<?php

namespace Admin\Models\ims;
use Admin\Models\BaseModel;
use Helpers\Cache;

class XHImsProduceType extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iPTId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sType;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSchema("xh_webmanage");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHIms_ProduceType';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsProduceType[]|XHImsProduceType|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsProduceType
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 获取商品类型枚举
     *
     * @return array|bool|mixed|null
     *
     * create by xjq 20180711
     */
    public static function getProduceTypeEnum(){
        if (Cache::exists('ImsProdType')) {
            return Cache::get('ImsProdType');
        }
        $enumArr = XHImsProduceType::find([
            'columns'    => 'iPTId,sType',
            'conditions' => 'bDelete = 0'
        ])->toArray();
        $enumArr = array_column($enumArr, 'sType', 'iPTId');
        Cache::save('ImsProdType', $enumArr);
        return $enumArr;
    }


    public function beforeUpdate()
    {
        $this->delCache();
        return parent::beforeUpdate(); // TODO: Change the autogenerated stub
    }


    public function beforeCreate()
    {
        $this->delCache();
        return parent::beforeCreate(); // TODO: Change the autogenerated stub
    }

    public function delCache(){
        if (Cache::exists('ImsProdType')) {
            Cache::delete('ImsProdType');
        }
        if (Cache::exists('Ims_ProdParts')) {
            Cache::delete('Ims_ProdParts');
        }
        if (Cache::exists('Ims_Produce')) {
            Cache::delete('Ims_Produce');
        }
    }
}
